package cn.niit.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxyFactory {
    /**
     * 被代理的目标对象
     * @param target
     */
    Object target;

    public DynamicProxyFactory(Object target) {
        this.target = target;
    }
    public Object getProxy(){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("即将调用["+method.getName()+"]......");
                        Object returnVal=method.invoke(target,args);
                        System.out.println("["+method.getName()+"]执行已完成");

                        return  returnVal;
                    }

                }
        );
    }
}
